package com.csnf.hymall.servlet;

import com.alibaba.fastjson.JSONObject;
import com.csnf.hymall.entity.Admin;
import com.csnf.hymall.service.AdminService;
import com.csnf.hymall.service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

@WebServlet(urlPatterns = "/admin/login")
public class AdminLoginServlet extends BaseServlet {
    private final Logger logger = Logger.getLogger(String.valueOf(AdminLoginServlet.class));
    private AdminService adminService;

    @Override
    public void init() throws ServletException {
        super.init();
        adminService = new AdminServiceImpl();
    }

    @Override
    protected void doService(HttpServletRequest req, HttpServletResponse resp) {

        try {
            switch (action) {
                case "doLogin":
                    checkLogin(req, resp);
                    break;
                case "profile_picture":
                    getAdminProfilePicture(req, resp);
                    break;
                case "":
                    skipToPage(req, resp, "admin/loginPage.jsp");
                    break;
                default:
                    skipToPage(req, resp, "admin/loginPage.jsp");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }


    private void checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("管理员登录验证");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Admin admin = adminService.login(username, password);
        JSONObject object = new JSONObject();
        if (admin == null) {
            logger.info("登录验证失败");
            object.put("success", false);
        } else {
            logger.info("登录验证成功，管理员ID传入会话");
            HttpSession session = request.getSession();
            session.setAttribute("adminId", admin.getAdmin_id());
            object.put("success", true);
        }
        returnJson(response, object);
    }

    private void getAdminProfilePicture(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("根据用户名获取管理员头像路径");
        String username = request.getParameter("username");
        Admin admin = adminService.get(username, null);
        JSONObject object = new JSONObject();
        if (admin == null) {
            logger.info("未找到头像路径");
            object.put("success", false);
        } else {
            logger.info("成功获取头像路径");
            object.put("success", true);
            object.put("srcString", admin.getAdmin_profile_picture_src());
        }
        returnJson(response, object);
    }


}
